const http = require('http')
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const MySQLStore = require('express-mysql-session');
const logger = require('morgan');
const mysql = require('mysql');
const socketio = require('socket.io')

const homeRouter = require('./routes/home');
const userRouter = require('./routes/user');
const mapRouter = require('./routes/map');
const {log} = require("debug");
const parseurl = require("parseurl");

const app = express();
const server = http.createServer(app)
const io = socketio(server)
app.set('io', io)

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

let config = {
    host: 'localhost',
    port: 3306,
    user: 'root',
    password: 'thechosen2!',
    database: 'campus_help'
}

let connection = mysql.createConnection(config)
connection.connect();

app.set('connection', connection)

let sessionStore = new MySQLStore(config)

app.use(session({
    secret: 'danzi',
    store: sessionStore,
    resave: true,
    saveUninitialized: false,
    cookie: {
        maxAge: 1000 * 60 * 60 * 24
    }
}))

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, '/public/')));

app.use('/', homeRouter);

// login and register
app.use('/user', userRouter);

// map
app.use('/map', mapRouter);

const PORT = 3000

server.listen(PORT, () => console.log(`Server running on port ${PORT}`))
